package com.cxj.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.cxj.musicplayer.activity.SongsListActivity;
import com.cxj.musicplayer.activity.TitleActivity;
import com.cxj.musicplayer.adapter.PlayListAdapter;
import com.cxj.musicplayer.data.ActivityManager;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.musicsource.network.DownloadFrom163;
import com.cxj.musicplayer.service.MediaService;

public class MainActivity extends TitleActivity {

    private GridView gridView;
    final int UPDATE_PLAY_LIST = 1;

    public GridView gridView(){
        return gridView;
    }


    @SuppressLint("HandlerLeak")
    private Handler handle = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATE_PLAY_LIST:
                    Bundle bundle = msg.getData();
                    PlayListAdapter adapter = new PlayListAdapter(MainActivity.this, bundle.getString("play_list"));
                    gridView.setAdapter(adapter);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityManager.activities.put(getClass(), this);

        gridView = findViewById(R.id.gridview);
        gridView.setOnItemClickListener(new GridView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, DataManager.array_data.get("play_list_title")[i], Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, SongsListActivity.class);
                intent.putExtra("position", i);
                MainActivity.this.startActivity(intent);
            }
        });


        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Bundle bundle = new Bundle();
                    bundle.putString("play_list", DownloadFrom163.getPlayList());
                    Message message = new Message();
                    message.what = UPDATE_PLAY_LIST;
                    message.setData(bundle);
                    handle.sendMessage(message);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }


    @Override
    protected void onDestroy() {
        ActivityManager.activities.remove(getClass());
        super.onDestroy();
    }
}
